﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.Configuration;
using Stone.Library.Common;

namespace Stone.Host
{
    public partial class FormHost : Form
    {
        string key = ConfigurationManager.AppSettings["IpPort"].ToString();

        public FormHost()
        {
            InitializeComponent();
            toolStripTextBox1.Text = key;
        }

        private void 开启服务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newIp = this.toolStripTextBox1.Text.Trim();
            if (!newIp.Equals(key))
            {
                ConfigHelp.setAppValue("IpPort", newIp);
                ConfigHelp.ChangeServiceConfig(newIp);
            }
            ServiceHostGroup.StartAllConfiguredServices();
            this.richTextBox1.AppendText("服务状态：已开启\n");
            MessageBox.Show("服务状态：已开启");
            服务状态toolStripLabel.Text = "服务状态：已开启 ";  
            开启服务ToolStripMenuItem.Enabled = false;
            开启服务ToolStripMenuItem1.Enabled = false;
            关闭服务ToolStripMenuItem.Enabled = true;
            关闭服务ToolStripMenuItem1.Enabled = true;
        }

        private void 关闭服务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ServiceHostGroup.CloseAllServices();
            this.richTextBox1.AppendText("服务状态：已关闭\n");
            MessageBox.Show("服务状态：已关闭");
            服务状态toolStripLabel.Text = "服务状态：已关闭 ";
            开启服务ToolStripMenuItem.Enabled = true;
            开启服务ToolStripMenuItem1.Enabled = true;
            关闭服务ToolStripMenuItem.Enabled = false;
            关闭服务ToolStripMenuItem1.Enabled = false;
        }

        private void FormHost_Shown(object sender, EventArgs e)
        {
            开启服务ToolStripMenuItem_Click(null, null);
        }

        private void MyNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.Visible)
            {
                this.Hide();
            }
            else
            {
                this.Show();
            } 
        }

        private void 打开主界面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.BringToFront();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
